# _*_ coding: utf-8 _*_
# .@FileName:rename_widget
# .@Data....:2025-02-05 : 11 : 21
# .@Aurhor..:LiuJingYu
"""
launch:
   import maya.cmds as cmds
   FileName.main() 
"""
from __future__ import unicode_literals, print_function

# 导入自定义的配置模块，可能包含一些全局配置信息
import config
from typing import Optional
# 从 PySide2.QtGui 模块导入所有内容，该模块包含了与图形用户界面相关的图形、字体、颜色等功能
from PySide2.QtGui import *
# 从 PySide2.QtCore 模块导入 Qt 类，该类包含了许多 Qt 框架的常量和枚举值
from PySide2.QtCore import Qt
# 从 PySide2.QtWidgets 模块导入所有内容，该模块包含了各种用户界面组件，如按钮、文本框等
from PySide2.QtWidgets import *

import rename


#
# class AddNamePrefix(QWidget):
#     def __init__(self, parent=None):
#
#         super(AddNamePrefix, self).__init__(parent)
#         self.main_layout = QVBoxLayout(self)
#
#         self.body_layout = QHBoxLayout()
#
#         self.prefix = QLineEdit()
#         self.do_bn = QPushButton("添加")
#         self.do_bn.clicked.connect(self.add_name_prefix)
#
#         self.body_layout.addWidget(QLabel("前缀: "))
#         self.body_layout.addWidget(self.prefix)
#         self.body_layout.addWidget(self.do_bn)
#
#         self.main_layout.addWidget(QLabel("添加前缀： "))
#         self.main_layout.addLayout(self.body_layout)
#
#     def add_name_prefix(self):
#         rename.add_name_prefix(self.prefix.text())
#
#
#
#     def paintEvent(self, *args, **kwargs):
#         # 创建一个 QPainter 对象，用于在窗口部件上进行绘制操作
#         p = QPainter(self)
#         # 设置画笔为无，即不绘制边框
#         p.setPen(Qt.NoPen)
#         # 创建一个画刷，颜色为 RGB(65, 65, 65)，并将其设置为绘制的填充颜色
#         p.setBrush(QBrush(QColor(65, 65, 65)))
#         # 绘制一个与窗口部件大小相同的矩形
#         p.drawRect(self.rect())
#         # 结束绘制操作
#         p.end()
#
#
# class ReplaceNameWidget(QWidget):
#     def __init__(self, parent=None):
#
#         super(ReplaceNameWidget, self).__init__(parent)
#
#         self.main_layout = QVBoxLayout(self)
#
#         self.search_layout = QHBoxLayout()
#         self.search = QLineEdit()
#         self.search_layout.addWidget(QLabel("搜索： "))
#         self.search_layout.addWidget(self.search)
#
#         self.replace_layout = QHBoxLayout()
#         self.replace = QLineEdit()
#         self.replace_layout.addWidget(QLabel("替换： "))
#         self.replace_layout.addWidget(self.replace)
#
#         self.do_bn_layout = QHBoxLayout()
#         self.search_replace_bn = QPushButton("搜索替换")
#         self.regex_search_replace_bn = QPushButton("正则搜索替换")
#         self.do_bn_layout.addWidget(self.search_replace_bn)
#         self.do_bn_layout.addWidget(self.regex_search_replace_bn)
#
#         self.search_replace_bn.clicked.connect(self.search_replace)
#         self.regex_search_replace_bn.clicked.connect(self.regex_search_replace)
#
#         self.main_layout.addWidget(QLabel("搜索替换名称: "))
#         self.main_layout.addLayout(self.search_layout)
#         self.main_layout.addLayout(self.replace_layout)
#         self.main_layout.addLayout(self.do_bn_layout)
#
#         self.main_layout.addStretch(0)
#
#     def search_replace(self):
#         rename.search_replace_name(self.search.text(), self.replace.text())
#
#     def regex_search_replace(self):
#         rename.regex_search_replace_name(self.search.text(), self.replace.text())
#
#     def paintEvent(self, *args, **kwargs):
#         # 创建一个 QPainter 对象，用于在窗口部件上进行绘制操作
#         p = QPainter(self)
#         # 设置画笔为无，即不绘制边框
#         p.setPen(Qt.NoPen)
#         # 创建一个画刷，颜色为 RGB(65, 65, 65)，并将其设置为绘制的填充颜色
#         p.setBrush(QBrush(QColor(65, 65, 65)))
#         # 绘制一个与窗口部件大小相同的矩形
#         p.drawRect(self.rect())
#         # 结束绘制操作
#         p.end()
#
#
# class RenameWidget(QWidget):
#     def __init__(self, parent=None):
#
#         super(RenameWidget, self).__init__(parent)
#         self.main_layout = QVBoxLayout(self)
#
#         self.add_name = AddNamePrefix(self)
#         self.replace_name = ReplaceNameWidget(self)
#
#         self.main_layout.addWidget(self.add_name)
#         self.main_layout.addWidget(self.replace_name)
#

class BaseWidget(QWidget):
    """自定义控件基类（提供统一背景色）"""

    def paintEvent(self, event):
        """
        重绘事件处理函数
        :param event: 绘制事件对象
        """
        painter = QPainter(self)
        painter.setPen(Qt.NoPen)
        painter.setBrush(QBrush(QColor(65, 65, 65)))  # 深灰色背景
        painter.drawRect(self.rect())
        painter.end()


class PrefixAdder(BaseWidget):
    """
    名称前缀添加组件
    功能：为选定对象添加指定前缀
    """

    def __init__(self, parent: Optional[QWidget] = None):
        """
        初始化组件
        :param parent: 父级控件
        """
        super().__init__(parent)

        # 主布局配置
        self._setup_ui()

    def _setup_ui(self):
        """初始化用户界面"""
        self.main_layout = QVBoxLayout(self)
        self.main_layout.setContentsMargins(5, 5, 5, 5)  # 统一边距

        # 输入区域布局
        input_layout = QHBoxLayout()
        self.prefix_input = QLineEdit()
        self.prefix_input.setPlaceholderText("输入前缀内容")
        self.btn_add = QPushButton("添加")
        self.btn_add.clicked.connect(self._add_prefix)

        # 组装输入区域
        input_layout.addWidget(QLabel("前缀:"))
        input_layout.addWidget(self.prefix_input)
        input_layout.addWidget(self.btn_add)

        # 最终布局
        self.main_layout.addWidget(QLabel("添加前缀："))
        self.main_layout.addLayout(input_layout)

    def _add_prefix(self):
        """处理添加前缀按钮点击事件"""
        try:
            prefix = self.prefix_input.text().strip()
            if not prefix:
                QMessageBox.warning(self, "警告", "请输入有效前缀内容")
                return

            rename.add_name_prefix(prefix)
            QMessageBox.information(self, "成功", "前缀添加操作已完成")

        except Exception as e:
            QMessageBox.critical(self, "错误", f"操作失败: {str(e)}")


class NameReplacer(BaseWidget):
    """
    名称替换组件
    功能：提供普通搜索替换和正则表达式替换功能
    """

    def __init__(self, parent: Optional[QWidget] = None):
        super().__init__(parent)
        self._setup_ui()

    def _setup_ui(self):
        """初始化用户界面"""
        self.main_layout = QVBoxLayout(self)
        self.main_layout.setContentsMargins(5, 5, 5, 5)

        # 搜索输入区域
        search_layout = QHBoxLayout()
        self.search_input = QLineEdit()
        self.search_input.setPlaceholderText("输入搜索内容")
        search_layout.addWidget(QLabel("搜索:"))
        search_layout.addWidget(self.search_input)

        # 替换输入区域
        replace_layout = QHBoxLayout()
        self.replace_input = QLineEdit()
        self.replace_input.setPlaceholderText("输入替换内容")
        replace_layout.addWidget(QLabel("替换:"))
        replace_layout.addWidget(self.replace_input)

        # 按钮区域
        btn_layout = QHBoxLayout()
        self.btn_normal = QPushButton("普通替换")
        self.btn_regex = QPushButton("正则替换")
        self.btn_normal.clicked.connect(self._normal_replace)
        self.btn_regex.clicked.connect(self._regex_replace)

        btn_layout.addWidget(self.btn_normal)
        btn_layout.addWidget(self.btn_regex)

        # 组装界面
        self.main_layout.addWidget(QLabel("搜索替换名称:"))
        self.main_layout.addLayout(search_layout)
        self.main_layout.addLayout(replace_layout)
        self.main_layout.addLayout(btn_layout)
        self.main_layout.addStretch()

    def _validate_inputs(self) -> bool:
        """验证输入有效性"""
        if not self.search_input.text().strip():
            QMessageBox.warning(self, "警告", "搜索内容不能为空")
            return False
        return True

    def _normal_replace(self):
        """执行普通替换操作"""
        if not self._validate_inputs():
            return

        try:
            rename.search_replace_name(
                self.search_input.text(),
                self.replace_input.text()
            )
            QMessageBox.information(self, "成功", "普通替换操作已完成")
        except Exception as e:
            QMessageBox.critical(self, "错误", f"普通替换失败: {str(e)}")

    def _regex_replace(self):
        """执行正则表达式替换操作"""
        if not self._validate_inputs():
            return

        try:
            rename.regex_search_replace_name(
                self.search_input.text(),
                self.replace_input.text()
            )
            QMessageBox.information(self, "成功", "正则替换操作已完成")
        except Exception as e:
            QMessageBox.critical(self, "错误", f"正则替换失败: {str(e)}")


class RenameManager(BaseWidget):
    """
    重命名管理主界面
    功能：整合前缀添加和名称替换功能
    """

    def __init__(self, parent: Optional[QWidget] = None):
        super().__init__(parent)
        self._setup_ui()

    def _setup_ui(self):
        """初始化用户界面"""
        self.main_layout = QVBoxLayout(self)
        self.main_layout.setContentsMargins(0, 0, 0, 0)
        self.main_layout.setSpacing(5)  # 控件间距

        # 创建子组件
        self.prefix_adder = PrefixAdder()
        self.name_replacer = NameReplacer()

        # 组装界面
        self.main_layout.addWidget(self.prefix_adder)
        self.main_layout.addWidget(self.name_replacer)


def create_rename_tool() -> RenameManager:
    """
    创建重命名工具主界面
    :return: 配置完成的 RenameManager 实例
    """
    return RenameManager()

def new():
    return RenameManager()
